package com.siruier.boss.ui.activity.opencard

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.kaiKa
import com.siruier.boss.bean.City
import com.siruier.boss.bean.NumberListVO
import com.siruier.boss.databinding.ActivityOpenCardCitySelectBinding
import com.siruier.boss.databinding.ItemOpenCardCityBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.ext.showEmptyCallback
import com.siruier.boss.ui.ext.showLoadingBeforeSuccessCallback
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper

@SuppressLint("NotifyDataSetChanged")
class OpenCardNumberSelectActivity : BaseActivity<ActivityOpenCardCitySelectBinding>() {
    private val city: City by InjectBundle()
    private var page: Int = 1
    private lateinit var mLoadService: LoadService<*>
    private val mAdapter = OpenCardNumberAdapter()

    override fun initView() {
        mLoadService = LoadSirHelper.register(vb.loadview, listener = ::onReload)

        bindViewClick(vb.tvChange)
        vb.tvChange.isVisible = true

        mAdapter.setOnClickAdapterListenter(::onClick)
        vb.rvProvince.adapter = mAdapter
        vb.rvProvince.layoutManager = LinearLayoutManager(this)
        vb.rvProvince.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        vb.rvCity.isVisible = false

        onReload()
    }

    private fun onReload() {
        mLoadService.showLoadingBeforeSuccessCallback()
        launchUI({
            val numberListVOs = kaiKa.numberList(city.code, page).toData()
            if (!numberListVOs.isNullOrEmpty()) {
                mAdapter.datas = numberListVOs
                mAdapter.notifyDataSetChanged()
            }
        }, ApiObserver(loadService = mLoadService))
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.tvChange -> {
                page++
                onReload()
            }
        }
    }

    private fun onClick(view: View, position: Int) {
        val province = mAdapter.getItem(position)

        mAdapter.datas.forEachIndexed { index, city ->
            if (index != position && city.check) {
                city.check = false
                mAdapter.notifyItemChanged(index)
                return@forEachIndexed
            }
        }
        if (!province.check) {
            province.check = true
            mAdapter.notifyItemChanged(position)
        }

        launchStartActivityForResult(OpenCardSubmitActivity::class.java, options = {
            putParcelable("city", city)
            putParcelable("number", province)
        }) {
            onReload()
        }
    }
}

class OpenCardNumberAdapter : BaseRecyclerAdapter<ItemOpenCardCityBinding, NumberListVO>() {
    override fun onBindViewHolder(vb: ItemOpenCardCityBinding, bean: NumberListVO, position: Int) {
        vb.tvText.gravity = Gravity.CENTER
        vb.tvText.setTextColor(if (bean.check) R.color.main_color.toColor() else R.color.text_color.toColor())
        vb.tvText.text = bean.mobileno
        setViewClick(position, vb.root)
    }
}